#include "bits/stdc++.h"
using namespace std;
struct MaxFlowGraph {
  using i64 = long long;
  const i64 inf = 0x3f3f3f3f3f3f3f3f;
  struct Edge {
    int next_, to_;
    i64 wi_;
  };

  MaxFlowGraph(int S, int T, int n, int m) : S_(S), T_(T), idx_(0), edges_(2 * m + 1), 
                                    cur_(n + 1, -1), head_(n + 1, -1), d_(n + 1, -1) {}

  void addEdge(int x, int y, i64 w, i64 rw = 0) {
    edges_[idx_] = {head_[x], y, w};
    head_[x] = idx_;
    idx_++;
    edges_[idx_] = {head_[y], x, rw};
    head_[y] = idx_;
    idx_++;
  }

  bool bfs() {
    for (auto &p : pos_) {
      d_[p] = -1;
    }
    pos_.clear();
    queue<int> q;
    pos_.emplace_back(S_);
    cur_[S_] = head_[S_];
    d_[S_] = 0;
    q.push(S_);

    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = head_[u]; ~i; i = edges_[i].next_) {
        int j = edges_[i].to_;
        i64 w = edges_[i].wi_;
        if (d_[j] == -1 && w) {
          d_[j] = d_[u] + 1;
          cur_[j] = head_[j];
          pos_.emplace_back(j);
          if (j == T_) {
            return true;
          }
          q.push(j);
        }
      }
    }
    return false;
  }

  i64 dfs(int u, i64 limit) {
    if (u == T_) {
      return limit;
    }
    i64 mx_flow = 0;

    for (int i = cur_[u]; ~i && mx_flow < limit; i = edges_[i].next_) {
      int j = edges_[i].to_;
      i64 &w = edges_[i].wi_;
      cur_[u] = i;
      if (d_[j] == d_[u] + 1 && w) {
        i64 tmp_flow = dfs(j, min(w, limit - mx_flow));
        if (!tmp_flow) {
          d_[j] = -1;
        } else {
          w -= tmp_flow;
          edges_[i ^ 1].wi_ += tmp_flow;
          mx_flow += tmp_flow;
        }
      }
    }
    return mx_flow;
  }

  i64 work() {
    i64 mx_flow = 0, flow = 0;
    while (bfs()) {
      while (flow = dfs(S_, inf)) {
        mx_flow += flow;
      }
    }
    return mx_flow;
  }

  vector<Edge> edges_;
  vector<int> cur_, d_, head_, pos_;
  int S_, T_, idx_;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  MaxFlowGraph flow(s, t, n, m);
  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    flow.addEdge(x, y, w);
  }
  cout << flow.work() << endl;
  return 0;
}